<?php

namespace App\Http\Controllers\Wap;

use App\Service\Category;
use App\Repositories\Category as CategoryRepository;
use App\Post;
use App\Setting;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PostController extends Controller
{
    //
    private $postManager;
    private $settingManager;
    private $categoryManager;
    private $categoryRepository;
    private $request;
    public function __construct (Request $request)
    {
        $this->postManager = new Post();
        $this->settingManager = new Setting();
        $this->categoryManager = new Category();
        $this->categoryRepository = new CategoryRepository();
        $this->request = $request;
    }

    /**
     *文章列表
     */
    public function index($slug){
        $category = $this->categoryRepository->where('slug',$slug)->where('status',Category::DISPLAY)->first();
        if($category){
            $category = $category->toArray();
        }else{
            abort(404);
        }
        //获得栏目模板
        if(!$category['templist']&&$category['pid']){
            $parent = $this->categoryRepository->select('templist')->where('status',Category::DISPLAY)->where('id',$category['pid'])->first();
            if($parent){
                $parent = $parent->toArray();
                if($parent['templist'])$category['templist'] = $parent['templist'];
            }else{
                $category['templist'] = 'index.blade.php';
            }
        }
        $template = $category['templist']?:'index.blade.php';
        $template = preg_replace('/.blade.php/','',$template);
        $banners = getPostsByCategory($slug,['order'=>'sort','length'=>9,'article_attr'=>'f']);
        return view('web.posts.list.'.$template,
            [
                'slug'=>$slug,
                'request'=>$this->request,
                'category'=>$category,
                'banners'    =>  $banners
            ]
        );
    }
    public function ajaxList(){
        $id = $this->request->category_id;
        $category = $this->categoryRepository->where('id',$id)->where('status',Category::DISPLAY)->first();
        if($category){
            $category = $category->toArray();
        }else{
            abort(404);
        }
        //获得栏目模板
        if(!$category['templist']&&$category['pid']){
            $parent = $this->categoryRepository->select('templist')->where('status',Category::DISPLAY)->where('id',$category['pid'])->first();
            if($parent){
                $parent = $parent->toArray();
                if($parent['templist'])$category['templist'] = $parent['templist'];
            }else{
                $category['templist'] = 'index.blade.php';
            }
        }
        $template = $category['templist']?:'index.blade.php';
        $template = preg_replace('/.blade.php/','',$template);

        switch ($category['cross']){
            case 0:

                break;
            case 1:
                //获取同名栏目
                $crossids = $this->categoryRepository->where('status',Category::DISPLAY)->where('cat_name',$category['cat_name'])->pluck('id');
                $this->request->crossId = $crossids;
                break;
            case 2:
                $this->request->crossId = array_merge(explode(',',$category['crossid']),[$id]);
                break;
            default:
                break;
        }
        $this->request->category_id = $category['id'];
        $this->request->status = Post::PUBLISHED;
        $list =  $this->postManager->getPageList($this->request,true);
        if($list){
            return view('web.posts.list-components.'.$template,[
                'list' =>  $list
            ]);
        }
        else{
            return '';
        }
    }

    /**
     * @param $id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail($id){
        $post = $this->postManager->getPost($id);
        if(!$post){
            abort(404);
        }
        if($post->use_link==Post::USER_LINK){
            if(isset($post->link)&&$post->link) return redirect($post->link);
        }
        $template = $post['tpl']?:'';
        if(!$template) {
            //查询栏目主题
            $category = $this->categoryRepository->where('id', $post['category_id'])->where('status',Category::DISPLAY)->first();
            if(!$category){
                abort(404);
            }else{
                $category = $category->toArray();
            }
            if (!$category['tempindex']&&$category['pid']!=0) {
                $parent = $this->categoryRepository->select('tempindex')->where('status',Category::DISPLAY)->where('id', $category['pid'])->first();
                if(!$parent){
                }else{
                    $parent = $parent->toArray();
                    if ($parent['tempindex']) $category['tempindex'] = $parent['tempindex'];

                }
            }
            if($category['tempindex']){
                $template = $category['tempindex'];

            }else{
                $template = 'index.blade.php';
            }
        }
        $template = preg_replace('/.blade.php/','',$template);
        return view('web.posts.view.'.$template,[
            'post'  =>  $post,
        ]);
    }

    public function channel($slug){
    }
}
